/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.service;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.dao.DBQuery;
import com.cunyu.util.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * suser 业务相关
 * @author win
 * 2019/04/26-17:05
 */
@Service
public class SuserService extends BaseService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SuserService.class) ;

    public RespData<List<Map<String, Object>>> getPage(ReqData reqData, Form form) {
        reqData.setOrder("desc");
        reqData.setSort("s.ctime");
        String sql = "select s.id,s.tel,s.nick,s.state,s.ctime,u.idcard,u.citycode,u.name," +
                "u.city,u.idimgs,u.atype,u.agency,u.department,u.imgs,u.icon,u.state as check from suser s,userinfo u " +
                " where s.id = u.uid " ;
        DBQuery query = new DBQuery(sql) ;
        if(StringUtil.hasLength(form.getUname())){
            query.and(" u.name like ?").setParams("%"+ form.getUname() +"%") ;
        }
        if(StringUtil.hasLength(form.getTel())){
            query.and(" s.tel = ? ").setParams(Long.valueOf(form.getTel())) ;
        }
        if(StringUtil.hasLength(form.getState())){
            query.and(" s.state = ? ").setParams(Integer.valueOf(form.getState())) ;
        }
        if(StringUtil.hasLength(form.getCheck())){
            query.and(" u.state = ? ").setParams(Integer.valueOf(form.getCheck())) ;
        }
        return page(query.getSql(),reqData,query.getParams()) ;
    }

    public Map<String, Object> getUser(int id) {
        String sql = "select s.id,s.tel,s.nick,s.state,s.ctime,s.inviteid,u.idcard,u.citycode,u.name," +
                " u.city,u.idimgs,u.atype,u.agency,u.department,u.imgs,u.icon,u.state as check," +
                " a.money,a.score,(select f.name from userinfo f where f.uid = s.inviteid) fname ," +
                "(select count(*) from myorder m where m.uid = s.id) allnum ," +
                "(select count(*) from feedback f where f.uid = s.id and f.oid > 0) fdnum ," +
                "(select count(*) from myorder m where m.uid = s.id and m.state = 3 ) renum from suser s " +
                "left join userinfo u on u.uid = s.id left join account a on a.uid = s.id where s.id = ?" ;
        return getMap(sql,id) ;
    }

    /**
     * 设置禁用 或 启用， 将 token 改变
     */
    public void disable(int state, int id) {
        String sql = "update suser set state = ? , token = ? where id = ?" ;
        exec(sql,state, ObjectId.get().toLong(),id) ;
    }

    /**
     * 审核用户提交数据是否符合
     * 通过 ，为用户和邀请人 送 抢单券
     * 不通过，则 设置状态为不通过，重新提交审核信息后，前端将状态改为待审核，则可继续审核
     * 已通过的用户，不能修改其审核信息。 不能将审核状态改为其他状态 ！！
     * 否则会多送券。
     */
    @Transactional(rollbackFor = Exception.class)
    public int check(int id, int state) {
        String infoSql = "select * from suser where id = ? " ;
        Map<String, Object> map = getMap(infoSql, id);
        String cSql = "update userinfo set state = ? where uid = ? " ;
        if(state == 1){
            // 成功，送券的逻辑
            int pid = (int) map.get("inviteid") ;
            String sql = "insert into ucoupon (uid,state,type) values (?,0,0)" ;
            exec(sql,id) ;
            if(pid != 0){
                exec(sql,pid) ;
            }
            exec(cSql,1,id) ;
            return 1 ;
        }else if(state == 2){
            exec(cSql,2,id) ;
            return 2 ;
        }else{
            throw new RuntimeException("unknown state : "+state) ;
        }
    }
}
